package com.example.common.tools

import com.alibaba.fastjson.JSON
import com.alibaba.fastjson.JSONArray
import com.alibaba.fastjson.JSONAware
import com.alibaba.fastjson.JSONObject
import com.google.gson.Gson

class JsonUtil {

    companion object {
        val instance by lazy(LazyThreadSafetyMode.SYNCHRONIZED) {
            JsonUtil()
        }
    }


    fun toJsonArray(list: List<*>): JSONArray {
        return JSONArray.parseArray(JSON.toJSONString(list))
    }

    fun <T> toObj(json: String, clazz: Class<T>): T {
        var jsonObject = JSON.parseObject(json)
        return JSON.toJavaObject(jsonObject, clazz)
    }

    fun mapToJson(map: Map<String, Any>):String {
        return JSON.toJSONString(map)
    }

    fun <T> toList(jsonArray: String?, clazz: Class<T>): List<T> {
        return if (jsonArray == null) {
            ArrayList<T>()
        } else {
            JSON.parseArray(jsonArray, clazz)
        }
    }
}

